/** 工具类 */

/**
 * 时间格式化 
 * @param date 日期
 * @param format 格式 
 * @returns string
 */
export const formatDate = (date: Date | string | number, format: string = 'YYYY-MM-DD HH:mm:ss'): string => {
    const d = new Date(date);
    const year = d.getFullYear();
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');
    const hours = String(d.getHours()).padStart(2, '0');
    const minutes = String(d.getMinutes()).padStart(2, '0');
    const seconds = String(d.getSeconds()).padStart(2, '0');

    return format
        .replace('YYYY', String(year))
        .replace('MM', month)
        .replace('DD', day)
        .replace('HH', hours)
        .replace('mm', minutes)
        .replace('ss', seconds)

}

/**
 * 引入本地图片
 * @param {*} fileName
 * @returns 
 */
export const getAssetsFile = (fileName:string) => {
    return new URL(`../assets/images/${fileName}`, import.meta.url).href // 使用vite推荐的方法渲染
  }

/**防抖 */
// export const debounce = <T extends (...args: any[]) => any>(func: T, delay: number = 1000)
//     : ((...args: Parameters<T>) => void) => {
//     let timeoutId: number;
//     const context = this;
//     return function (...args: Parameters<T>) {
//         timeoutId && clearTimeout(timeoutId)
//         timeoutId = setTimeout(() => {
//             func.apply(context, args)
//         }, delay)
//     }
// }


export const isObject = (obj:any):boolean =>{
    return obj != null &&  typeof obj  == 'object' && Object.keys(obj).length > 0
}

/**
 * 获取url上的参数
 * @param paramsKey 
 * @returns 
 */
export const getQueryVariable =(paramsKey:string):{[key:string]:string} | string =>{
    const query = window.location.search.slice(1)
    let arr = query.split('&')
    let obj:{[key:string]:string} = {}
    for (let i = 0; i < arr.length; i++) {
         let pair = arr[i].split('=')
        obj[pair[0]] =pair[1] 
    }
    if(paramsKey){
        return obj[paramsKey]
    }else{
        return obj
    }
}

